package hotel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;

public class FinestraCheckOut extends JFrame implements ActionListener {
	private ResultSet rs = null;
	private String[] clientiInCamera;
	private String numCamera;
	private int row;
	private JPanel p, p1, p2, p3;
	private JButton elimina, eliminaTutto;
	private JScrollPane scroll;
	private JList lista;
	private JLabel cameraL;
	private int numPostiOccupati;

	public FinestraCheckOut(int row) throws SQLException {
		this.row = row;
		setSize(400, 400);
		setResizable(false);
		setLocation(350, 150);
		setVisible(true);
		p = new JPanel();
		p.setLayout(new BorderLayout());
		add(p);
		p1 = new JPanel();
		p1.setLayout(new GridLayout(20, 20, 0, 0));
		p2 = new JPanel();
		p3 = new JPanel();
		p1.add(new JLabel("CAMERA:"));
		cameraL = new JLabel("Camera: "
				+ GraficaHotel.modelCamere.getValueAt(row, 0));
		numCamera = (String) GraficaHotel.modelCamere.getValueAt(row, 0);
		p1.add(cameraL);
		p.add(p1, BorderLayout.WEST);
		p.add(p2, BorderLayout.EAST);
		p.add(p3, BorderLayout.SOUTH);

		elimina = new JButton("Elimina");
		elimina.addActionListener(this);
		p3.add(elimina);
		eliminaTutto = new JButton("EliminaTutto");
		eliminaTutto.addActionListener(this);
		p3.add(eliminaTutto);
		clienti();
		for (int i = 0; i < clientiInCamera.length; i++) {
			rs = GraficaHotel.database.stat
					.executeQuery("SELECT nome,cognome FROM CLIENTI where numerodidocumento='"
							+ clientiInCamera[i] + "'");
			while (rs.next()) {
				clientiInCamera[i] = rs.getString(1) + " " + rs.getString(2);
			}
		}
		lista = new JList(clientiInCamera);
		scroll = new JScrollPane(lista);
		scroll.setAutoscrolls(true);
		lista.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		lista.setPreferredSize(new Dimension(100, 200));
		p2.add(new JLabel("CLIENTI NELLA CAMERA:"));
		p2.add(scroll);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == elimina) {

		} else if (e.getSource() == eliminaTutto) {
			lista.removeAll();
			try {
				GraficaHotel.database.stat
						.executeUpdate("UPDATE CAMERE SET stato='DISPONIBILE', numpostioccupati=0 where numero='"
								+ numCamera + "'");
				GraficaHotel.database.stat
						.execute("DELETE FROM ASSOCIAZIONE where camera='"
								+ numCamera + "'");
				FinestraCheckIn.aggiornaTabellaCamere();
			
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			ImageIcon icona=new ImageIcon("Risorse/v5.png");
			JOptionPane.showMessageDialog(null, "Tutti i clienti sono stati rimossi dalla camera.","Ottimo",JOptionPane.INFORMATION_MESSAGE,icona);
			dispose();
		}
	}

	private void clienti() throws SQLException {
		rs = GraficaHotel.database.stat
				.executeQuery("SELECT numerodocumento FROM ASSOCIAZIONE where camera='"
						+ GraficaHotel.modelCamere.getValueAt(row, 0) + "'");
		rs.last();
		clientiInCamera = new String[rs.getRow()];
		clientiInCamera[0] = rs.getString(1);
		int i = 1;
		while (rs.previous()) {
			clientiInCamera[i] = rs.getString(1);
			i++;
		}
	}
}
